import getCurrentUri from './getCurrentUri.js';
import {handleShareActInfo} from './actLog.js';
export default (shareInfo, source,pageData,props) => {
	var path = '',params = [];
	if (shareInfo.ifCurrent) {
		path = getCurrentUri(shareInfo.params.map(obj=>obj.key))
	} else {
		path = '/views/singlePage?pageId=' + shareInfo.pageId
	}
	shareInfo.params.forEach(obj => {
		if (obj.source === "static") {
			params.push(obj.key + '=' + obj.val)
		} else if (obj.source === 'variable') {
			var variable = [];
			if(source === 'indexPage'){
				var originSelectIndex = pageData.appMenus[0].common.originSelectIndex;
				var pageId = pageData.appMenus[0].common.links[originSelectIndex].pageId;
				pageData.conData.some(page=>{
					if (page.cid === pageId && page.variable){
						variable = page.variable
						return true
					}
				})
			} else if (source === 'singlePage'){
				variable = pageData.conData[0].variable || []
			}
			variable.some(vari => {
				if (vari.keyName === obj.val) {
					params.push(obj.key + '=' + vari.val)
					return true
				}
			})
		} else if (obj.source === 'storage') {
			var storageVal = '';
			// #ifdef H5
			storageVal = localStorage.getItem(obj.val);
			// #endif
			// #ifdef MP
			storageVal = uni.getStorageSync(obj.val);
			// #endif
			params.push(obj.key + '=' + storageVal);
		}else if(obj.source === 'props' && props){
			params.push(obj.key + '=' + props[obj.val])
		}
	})
	// 如果此分享事件加了发送链接的埋点，则在分享参数要额外加上埋点的传播参数
	let shareActObj = {},ifHasActLogParam = Boolean(shareInfo.tranConfig);
	if(ifHasActLogParam){
		shareActObj = handleShareActInfo(shareInfo.tranConfig, props, path)
		params.push('tranSource=chatList')
		params.push('shareKrtNo='+shareActObj.krtNo)
		params.push('tranRootUuid='+shareActObj.shareActInfo.tranRootUuid)
		params.push('tranPuuid='+shareActObj.tranUuid)
		params.push('tranLevel='+shareActObj.shareActInfo.tranLevel)
	}

	params = params.join('&')
	if(params){
		if (path.indexOf('?') > -1) {
			path += ('&' + params)
		} else {
			path += ('?' + params)
		}
	}
	return path
}
